package eu.codlab.androidemu.gbcdroid;


import java.io.File;
import java.io.FileFilter;

import eu.codlab.androidemu.ActivitiesHelper;
import eu.codlab.androidemu.ActivitiesHelperGBA;
import eu.codlab.androidemu.ActivitiesHelperGBC;
import eu.codlab.androidemu.origbc.FileSelectedListener;
import eu.codlab.androidemu.viewpager.TitleProvider;
import eu.codlab.src.game.R;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

public class MainPagerAdapter
extends PagerAdapter
implements TitleProvider, ListListener, FileFilter, View.OnKeyListener{
	private String [] _titles;
	private MainActivity _activity_main;
	private boolean _bigscreen;

	private ActivitiesHelper _helper;
	private ActivitiesHelperGBC _helperGBC;
	private ActivitiesHelperGBA _helperGBA;

	public MainPagerAdapter(boolean bigscreen, String path, MainActivity context){
		//TODO proper way
		_activity_main = context;
		_helper = new ActivitiesHelper(context, (FileSelectedListener)context, path);
		_helperGBC = new ActivitiesHelperGBC(context, (FileSelectedListener)context);
		_helperGBA = new ActivitiesHelperGBA(context, (FileSelectedListener)context);
		_bigscreen = bigscreen;
		if(bigscreen == true){
			_titles = new String[]{
					context.getString(R.string.list_gbac),
					context.getString(R.string.title_select_rom),
					context.getString(R.string.title_help)
			};
		}else{
			_titles = new String[]{
					context.getString(R.string.list_gba),
					context.getString(R.string.list_gbc),
					context.getString(R.string.title_select_rom),
					context.getString(R.string.title_help)
			};
		}


	}


	protected String[] getFileFilter() {
		return _helper.getFileFilter();
	}
	public void refresh(){
		_helper.refresh();
		_helperGBA.refresh();
		_helperGBC.refresh();
	}

	public void sdcard(){
		_helper.sdcard();
	}

	public void parent(){
		_helper.parent();
	}
	@Override
	public void destroyItem(View pager, int position, Object view) {
		try{
			((ViewPager)pager).removeView((View)view);
		}catch(Exception e){

		}
	}

	@Override
	public void finishUpdate(View container) {
	}

	@Override
	public int getCount() {
		return _titles.length;
	}

	@Override
	public Object instantiateItem(View pager, int position) {
		View v = null;
		LayoutInflater inflater = (LayoutInflater)_activity_main
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if(_bigscreen == true){
			switch(position){
			case 0:
				v = inflater.inflate(R.layout.file_chooser_list_gbc_gba, null);
				_helperGBA.setClickView(v);
				_helperGBC.setClickView(v);
				break;
			case 1:
				v = inflater.inflate(R.layout.file_chooser_list, null);

				_helper.setClickView(v);
				break;
			case 2:
				v = inflater.inflate(R.layout.file_chooser_help, null);
				break;
			}
		}else{
			switch(position){
			case 0:
				v = inflater.inflate(R.layout.file_chooser_list_gba, null);
				_helperGBA.setClickView(v);
				break;
			case 1:
				v = inflater.inflate(R.layout.file_chooser_list_gbc, null);
				_helperGBC.setClickView(v);
				break;
			case 2:
				v = inflater.inflate(R.layout.file_chooser_list, null);

				_helper.setClickView(v);
				break;
			case 3:
				v = inflater.inflate(R.layout.file_chooser_help, null);
				break;
			}
		}

		View v2 = v.findViewById(R.id.move);
		if(v2 != null && v2 instanceof TextView){
			((TextView)v2).setText(_activity_main.getString(R.string.move).replace("%s", new File(Environment.getExternalStorageDirectory(), "roms").getAbsolutePath()));
		}

		((ViewPager)pager).addView( v, 0 );
		return v;
	}

	protected void onFileSelected(Uri uri) {
		_activity_main.onFileSelected(uri);
	}

	@Override
	public void onListItemClick(String name, View v, int position, long id) {
		_helper.onListItemClick(name, v, position, id);
	}

	protected void onSaveInstanceState(Bundle outState) {
		if(_helper != null)
			_helper.onSaveInstanceState(outState);
	}




	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view.equals(object);
	}

	@Override
	public void restoreState(Parcelable state, ClassLoader loader) {
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(View container) {
	}

	@Override
	public String getTitle(int position) {
		return _titles[position];
	}

	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		return _helper.onKey(v, keyCode, event);
	}


	@Override
	public boolean accept(File pathname) {
		return _helper.accept(pathname);
	}
}
